package org.mozhu;

import com.github.dnbn.submerge.api.SubmergeAPI;
import com.github.dnbn.submerge.api.parser.ASSParser;
import com.github.dnbn.submerge.api.subtitle.ass.ASSSub;
import com.github.dnbn.submerge.api.subtitle.common.TimedLine;
import com.github.dnbn.submerge.api.subtitle.srt.SRTSub;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main {

    static String SOURCES_PATH = "/Users/parry.zhang/Workspace/git/mozhu/subtitle/docs/sources/";
    static String TARGET_PATH = "/Users/parry.zhang/Workspace/git/mozhu/subtitle/docs/targets/";

    public static void main(String[] args) throws Exception {



        Collection<File> files = FileUtils.listFiles(new File(SOURCES_PATH), new String[]{"ass"}, false);

        for (File file : files) {
            print(file);
        }
    }


    public static void print(File file) throws IOException {

        ASSParser parser = new ASSParser();
        ASSSub ass = parser.parse(file);

        SubmergeAPI api = new SubmergeAPI();
        SRTSub srt = api.toSRT(ass);


        List<String> lines = new ArrayList<>();
        lines.add("|  中文   | 英文  |");
        lines.add("|  ----  | ----  |");
        for (TimedLine timedLine : srt.getTimedLines()) {
            List<String> textLines = timedLine.getTextLines();
            if (textLines.size() != 2) {
//                System.out.println(textLines.get(0));
                continue;
            }

            lines.add("| " + textLines.get(0) + "  | " + textLines.get(1) + " |");
        }
        String content = String.join("\n", lines);
        System.out.println(content);

        String fileName = file.getName().replaceAll("\\.ass", ".md");
        FileUtils.write(new File(TARGET_PATH + fileName), content);
    }
}